#include <cmath>
#include "matrix.h"


Matrix::Matrix() {}
Matrix::Matrix(size_t rows, size_t cols) {
	setDimension(rows, cols);	
}

void Matrix::setDimension(size_t rows, size_t cols) {
 	_matrix= vector<vector<unsigned> >(rows, 
			vector<unsigned>(cols)); 
}

unsigned& Matrix::operator() (size_t row, size_t col) {
	return _matrix[row][col];
}

size_t Matrix::rows() {
	return _matrix.size();
}

size_t Matrix::cols() {
	return _matrix[0].size();
}

ostream& operator<<(ostream& o, Matrix& p) { 
/*   	o << p.rows() << " " << p.cols() << endl;    */

	for(size_t i= 0; i<p.rows(); i++) { 
		for(size_t j= 0; j<p.cols(); j++) {
			o << p(i,j) << " ";   
		}
   		o << endl;   
	} 

	return o; 
} 
istream& operator>>(istream& is, Matrix& p) { 
	size_t rows, cols;
	is >> rows;
	cols= rows;
	p.setDimension(rows, cols);
	
	for(size_t i= 0; i<rows; i++) {
		for(size_t j= 0; j<cols; j++) {
			double tmp;
			is >> tmp;
			unsigned y= (int)(round(tmp));
			if(y<1) y= 1;
			if(y>256) y= 256;
			p(i,j)= y;
		}
	}

	return is;
} 

